package com.hippo.core.service;


import com.alibaba.fastjson.JSONObject;
import com.hippo.core.dao.SceneDao;
import com.hippo.core.entity.Scene;
import com.hippo.core.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: hippo
 */
@Service
public class SceneService {

    @Autowired
    private SceneDao sceneDao;

    private final static String ROOT = "0";

    public static List<Scene> findRoots(List<Scene> allNodes) {
        // 根节点
        List<Scene> root = new ArrayList<>();
        allNodes.forEach(node -> {
            if (Objects.equals(node.getParentId(), ROOT)) {
                root.add(node);
            }
        });
        root.forEach(node -> {
            findChildren(node, allNodes);
        });
        return root;
    }

    private static Scene findChildren(Scene treeNode, List<Scene> treeNodes) {
        for (Scene it : treeNodes) {
            if (treeNode.getChildren() == null) {
                treeNode.setChildren(new ArrayList<>());
            }
            if (treeNode.getId().equals(it.getParentId())) {
                treeNode.getChildren().add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    public JSONObject getSceneList(String projectId) {
        return CommonUtil.successPage(findRoots(sceneDao.getSceneList(projectId)));
    }

    public JSONObject updateScene(JSONObject obj) {
        return CommonUtil.response(sceneDao.updateScene(obj));
    }

    public JSONObject addScene(JSONObject obj) {
        return CommonUtil.response(sceneDao.addScene(obj));
    }

    public JSONObject deleteScene(JSONObject obj) {
        return CommonUtil.response(sceneDao.deleteScene(obj));
    }

    public JSONObject getSceneVariableList(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int count = sceneDao.countSceneVariable(jsonObject);
        List<JSONObject> list = sceneDao.getSceneVariableList(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    public JSONObject updateSceneVariable(JSONObject obj) {
        Object i = sceneDao.checkSceneVariableExist(obj);
        String name = obj.getString("name");
        if (Integer.parseInt(i.toString()) == 1) {
            String s = sceneDao.sceneVariableNameIsExist(obj);
            if (s.equals(name)) {
                return CommonUtil.response(sceneDao.updateSceneVariable(obj));
            } else {
                return CommonUtil.errorJson("场景变量名在当前项目已经存在，请重新命名");
            }
        } else {
            return CommonUtil.response(sceneDao.updateSceneVariable(obj));
        }
    }

    public JSONObject addSceneVariable(JSONObject obj) {
        Object i = sceneDao.checkSceneVariableExist(obj);
        if (i == null) {
            return CommonUtil.response(sceneDao.addSceneVariable(obj));
        } else {
            return CommonUtil.errorJson("场景变量名在当前项目已经存在，请重新命名");
        }
    }

    public JSONObject deleteSceneVariable(String id) {
        return CommonUtil.response(sceneDao.deleteSceneVariable(id));
    }
}
